<?php
session_start ();
include_once ('../config.php');
include_once ('rennclient/RennClient.php');
$renn_client = new RennClient ( RENREN_APP_KEY, RENREN_APP_SECRET );
$renn_client->setDebug ( DEBUG_MODE );
$cate='chongwu';

/*$c=count($nameArr)-1;
$i=rand(0,$c);
$name = $nameArr[$i];*/
if(isset($_GET['name_num'])){
    $c_c = $_GET['name_num'];
    $name = $nameArr[$c_c];
}else{
    $c_c = shunxu(count($nameArr));
    $name = $nameArr[$c_c];
}
echo $name;

$c=count($contentArr)-1;
$i=rand(0,$c);
$content = $contentArr[$i];

/*ID有问题*/
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
mysql_select_db(DB_NAME,$con);
mysql_query("SET NAMES utf8");
$sql="select * from cai_uid where name='".$name."' and sns='renren'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$token = explode('_',$row['token']);
//$token1 = '245624|2.dkUhSTsbwc994TJ1NaC12JcUdV26H5mz.494108862.1387450015404_8eb87791b48042b68be8f14f74c4e663_hmac-sha-1';
//$token=explode('_',$token1);
$MAC_TOKEN = $token[0];
$MAC_KEY = $token[1];
$MAC_ALGORITHM = $token[2];
$accessToken = new AccessToken ( TokenType::MAC, $MAC_TOKEN, null, $MAC_KEY, $MAC_ALGORITHM );
$renn_client->authWithToken($accessToken);
//dump($renn_client);

/*获取我的信息*/
$user_service = $renn_client->getUserService ();
$user = $user_service->getUserLogin ();
dump($user);
echo '<br/>--------------------<br/>';
/*获取状态列表*/
$user_id=suiji($cate);
$status_service = $renn_client->getStatusService();
$user_status =$status_service->listStatus($user_id,1,1);
echo $user_id;
dump($user_status);
echo '<br/>--------------------<br/>';
/*while(!isset($user_status[0]['id'])){
    $user_id=suiji($cate);
    $status_service = $renn_client->getStatusService();
    $user_status =$status_service->listStatus($user_id,1,1);
    echo $user_id;
    dump($user_status);
}*/

if(isset($user_status[0]['id'])){
    echo '待评论';
    /*添加评论*/
    $comment_service = $renn_client->getCommentService();
    $user_comment =$comment_service->putComment($content,0,'STATUS',$user_id,$user_status[0]['id']);
    dump($user_comment);
    if(isset($user_comment['content'])){
        $text = 'http://www.renren.com/home#//status/status?id='.$user_id.'_____'.$name.'_____'.$c_c;
        writelog($text,0);
        echo '成功';
    }else{
        writelog('失败',1);
        echo '失败';
    }
}else{
    $sql2="UPDATE `cai_renren_fensi` SET `done` ='2' WHERE  user_id='".$user_id."';";
    $result2=mysql_query($sql2);
    writelog('【状态不存在】'.$user_id.'-'.$name,1);
    echo '状态不存在';
}
function writelog($str,$success)
{
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    date_default_timezone_set('PRC');
    $time = time();
    $sql="INSERT INTO `cai_log` (`id`, `success`, `content` , `data`,`from_from`,`sns`) VALUES (NULL,'".$success."','".$str."','".$time."','".get_domain_all()."','renren');";
    $result=mysql_query($sql);
    if($result){
        echo '记录数据成功<br/>';
    }else{
        echo '记录数据失败<br/>';
    }
}

function suiji($cate){
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    //$sql="select * from cai_renren_fensi where done='0' and cate='".$cate."' and sns = 'renren' ORDER BY rand() LIMIT 1"; 
    $sql="select * from cai_renren_fensi where done='0' and cate='".$cate."' LIMIT 1"; 
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $id=$row['id'];
    $user_id=$row['user_id'];
    $sql2="UPDATE `cai_renren_fensi` SET `done` ='1' WHERE  id='".$id."';";
    $result2=mysql_query($sql2);
    if($result2){
        return $user_id;
    }else{
        return '0';
    }   
    //return $user_id;
}
function shunxu($max_num){
    include_once( '../config.php' );
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql0="select * from cai_shunxu where from_from ='".get_domain_all()."'"; 
    $result0=mysql_query($sql0);
    $row0 = mysql_fetch_array($result0, MYSQL_ASSOC);
    if(isset($row0)&&$row0!=''){
        //echo '有';
    }else{
        //echo '无';
        $sql1="INSERT INTO `cai_shunxu` (`id`, `num`, `from_from`) VALUES (NULL,'0','".get_domain_all()."');";
        $result1=mysql_query($sql1);
    }
    $sql="select * from cai_shunxu where from_from ='".get_domain_all()."'";
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $num_data=$row['num']+1;
    if($max_num<=$num_data){
        $num_data = 0;
    }
    $sql2="UPDATE `cai_shunxu` SET `num` ='".$num_data."' WHERE from_from ='".get_domain_all()."'";
    $result2=mysql_query($sql2);
    if($result2){
        return $num_data;
    }else{
        return 'error';
    }  
}
?>
